#include "ConstantCU.h"
#include "CPU.h"
#include <iostream>

using namespace std;

	 CPU* ConstantCU::cpu;
	 short ConstantCU::RC_Pointer;
	 short ConstantCU::Constant;

ConstantCU::ConstantCU()
{
}

ConstantCU::~ConstantCU()
{
}

void ConstantCU::setCPU(CPU* proc)
{
	cpu = proc;
}
void ConstantCU::Floadlit()
{
	ConstantCU::loadParamII();
	short result = Constant ; 
//	cout << " Pre-result " << result;
	if((result&0x0400) > 0)
		result = result|0xf800;
//	cout << "loadlit: result " << result << " RC: "<< RC_Pointer << endl;
	cpu->setWBRegister(RC_Pointer);
	cpu->setWBValue(result);
	cpu->setshallWB(true);
//	cpu->setRegister(RC_Pointer, result);
}
void ConstantCU::Flcl()
{
	ConstantCU::loadParamIII();
	short result = Constant;
	result = result|(cpu->readRegister(RC_Pointer)&0xff00);
	//cout << "contant: " << hex << result << endl;
	//cout << "RC = " << RC_Pointer << endl;
	cpu->setWBRegister(RC_Pointer);
	cpu->setWBValue(result);
	cpu->setshallWB(true);
//	cpu->setRegister(RC_Pointer,result);
}
void ConstantCU::Flch()
{
	ConstantCU::loadParamIII();
	short result = Constant;
	result = (result << 8)|(cpu->readRegister(RC_Pointer)&0x00ff);
	cpu->setWBRegister(RC_Pointer);
	cpu->setWBValue(result);
	cpu->setshallWB(true);
//	cpu->setRegister(RC_Pointer,result);
}
void ConstantCU::loadParamII()
{
	unsigned short inst = cpu->getIR();
	RC_Pointer = (inst&0x3800) >> 11;
	Constant = inst&0x07ff;
}
void ConstantCU::loadParamIII()
{
	unsigned short inst = cpu->getIR();
	RC_Pointer = (inst&0x3800) >> 11;
	//cout << "AOBA " << hex << (inst&0x3800) << endl;
	Constant = inst&0x00ff;
}

